package com.exit66.jukebox.data;

/**
 * @author andyb
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

import java.sql.SQLException;

public class TrackCollection extends BVDatabaseCollection {
    
    public TrackCollection() {
        
        //_currentRecord = new Track();
        
    }
    
    public void list(int start, int count) {
        
        processList("ORDER BY track_name", start, count);
        
    }
    
    public void list(String trackNameSearch, int start, int count) {
        
        processList("WHERE track_name LIKE " + Qts(trackNameSearch) + " ORDER BY track_name", start, count);
        
    }
    
    public void listByLibrary(int libraryID) {
        
        processList("WHERE library_id = " + String.valueOf(libraryID) + " ORDER BY track_name", 0, 0);
        
    }
    
    public void listByArtist(int artistID, int start, int count) {
        
        processList("WHERE track.artist_id = " + String.valueOf(artistID) + " ORDER BY track_name", start, count);
        
    }
    
    public void listByAlbum(int albumID, int start, int count) {
        
        processList("WHERE track.album_id = " + String.valueOf(albumID) + " ORDER BY track_number, track_name", start, count);
        
    }
    
    public void listByArtistAlbum(String artistAlbumSearch, int start, int count) {
        
        String albumID = "-1";
        String artistID = "-1";
        
        if (artistAlbumSearch.indexOf(",") != -1) {
            
            artistID = artistAlbumSearch.substring(0, artistAlbumSearch.indexOf(","));
            albumID = artistAlbumSearch.substring(artistAlbumSearch.indexOf(",") + 1);
            
            processList("WHERE track.artist_id = " + artistID + " AND track.album_id = " + albumID + " ORDER BY track_number, track_name", start, count);
            
        }
        
    }
    
    public void listByPlaylist(int playlistID, int start, int count) {
        
        processList(", playlist_track WHERE track.track_id = playlist_track.track_id AND playlist_track.playlist_id = " + playlistID + " ORDER BY play_order", start, count);
        
    }
    
    public void listByQueue() {
        
        processList(", request_queue WHERE track.track_id = request_queue.track_id ORDER BY play_order", 0, 0);
        
    }
    
    private void processList(String whereClause, int start, int count) {
        
        if (openConnection()) {
        	String sql;
        	if (count == 0) 
        		sql = "SELECT ";
        	else
        		sql = "SELECT LIMIT " + start + " " + count;
        	
            _rs = retrieveData(sql + " track.*, album.album_name, album.multiple_artist_id, " + 
            		"artist.artist_name, artist.artist_order_name FROM track JOIN artist ON track.artist_id = artist.artist_id " + 
            		" JOIN album ON track.album_id = album.album_id " + whereClause);
            try {
                
                if (_rs.next()) {
                    _eof = false;
                } else {
                    _eof = true;
                }
                
            } catch (SQLException e) {
                
                _eof = true;
                
            }
            
        }
        
    }
    
    public void search(String nameSearch, boolean searchArtists, boolean searchAlbums,
            boolean searchTracks) {
    	search(nameSearch, searchArtists, searchAlbums, searchTracks, 0, 20);
    }
    public void search(String nameSearch, boolean searchArtists, boolean searchAlbums,
            boolean searchTracks, int start, int count) {
        if (openConnection()) {
            
            String where = " WHERE ";
            String sql = "SELECT LIMIT " + start + " " + count + " track.track_id, track_name, album_name, artist_name " +
              "FROM track JOIN artist ON track.artist_id = artist.artist_id " + 
                "JOIN album ON track.album_id = album.album_id ";
            if (searchTracks) {
                sql = sql.concat(where + "track_name LIKE " + Qts(nameSearch));
                where = " OR ";
            }
            if (searchArtists) {
                sql = sql.concat(where + "artist_name LIKE " + Qts(nameSearch));
                where = " OR ";
            }
            if (searchAlbums) {
                sql = sql.concat(where + "album_name LIKE " + Qts(nameSearch));
                where = " OR ";
            }
            sql = sql.concat(" ORDER BY artist_order_name, album_name, track_name ");
            _rs = retrieveData(sql);
            try {
                
                if (_rs.next()) {
                    _eof = false;
                } else {
                    _eof = true;
                }
                
            } catch (SQLException e) {
                
                _eof = true;
                
            }
            
        }        
                
    }
    
    protected void loadCurrent() {
        
        try {
        	
        	if (_currentRecord == null)
        		_currentRecord = new Track();
            
            ((Track)_currentRecord).setTrackID(_rs.getInt("track_id"));
            ((Track)_currentRecord).setLibraryID(_rs.getInt("library_id"));
            ((Track)_currentRecord).setArtistID(_rs.getInt("artist_id"));
            ((Track)_currentRecord).setAlbumID(_rs.getInt("album_id"));
            ((Track)_currentRecord).setTrackName(_rs.getString("track_name"));
            ((Track)_currentRecord).setFileName(_rs.getString("file_name"));
            ((Track)_currentRecord).setAlbumID(_rs.getInt("content_type_id"));
            ((Track)_currentRecord).setTrackNumber(_rs.getInt("track_number"));
            
            Album album = new Album();
            album.setAlbumID(_rs.getInt("album_id"));
            album.setAlbumName(_rs.getString("album_name"));
            album.setMultipleArtistID(_rs.getInt("multiple_artist_id"));
            ((Track)_currentRecord).setAlbum(album);
            
            Artist artist = new Artist();
            artist.setArtistID(_rs.getInt("artist_id"));
            artist.setArtistName(_rs.getString("artist_name"));
            artist.setArtistOrderName(_rs.getString("artist_order_name"));
            ((Track)_currentRecord).setArtist(artist);
                        
        } catch (SQLException e) {};
        
    }
    
}
